Ext.onReady(function(){
	//roleTree
	var roleStore = Ext.create('Ext.data.TreeStore', {
		proxy: {
			type: 'ajax',
			url: '/usercontrol/sysrole_list/'
		},
		autoLoad: true
	});

	var roleTree=Ext.create('Ext.tree.Panel',{
		title: '系统角色',
		store: roleStore,
		rootVisible : false, 
		autoScroll : true, 
		width:340,
		region:'west',
		split:true,
		dockedItems:[{
			xtype:'toolbar',
			items:[{
				xtype:'button',
				text:'添加',
				iconCls:'add',
				handler:function(){
					roleForm.getForm().reset();
					roleWin.show();
				}
			},{
				xtype:'button',
				text:'修改',
				iconCls:'edit',
				handler:function(){
					var selected = roleTree.getSelectionModel().getSelection();
					if(selected!=''){
						roleForm.getForm().reset();
						roleForm.getForm().setValues({
							id:selected[0].data.id,
							role_name:selected[0].data.text
						});
						roleWin.show();
	    			}else{
	    				Ext.Msg.alert('系统提示', '请选择需要修改的角色!');
	    			}						
				}
			},{
				xtype:'button',
				text:'删除',
				iconCls:'delete',
				handler:function(){
					var selected = roleTree.getSelectionModel().getSelection();
					if(selected !=''){
						Ext.MessageBox.confirm('提示', '确认删除吗?', function(btn){
							if(btn=='yes'){
								Ext.Ajax.request({
									url:'/usercontrol/sysrole_del/',
									method : 'post',
									params:{
										id:selected[0].data.id
									},
									success:function(response){
										var results =  Ext.decode(response.responseText);
										if(results.success){
											roleStore.load();
										}else{
											Ext.Msg.alert('系统提示', results.message);
										}
									}
								});
							}
						});
        			}else{
        				Ext.Msg.alert('系统提示', '请选择需要删除的角色!');
        			} 					
				}
			},{
				xtype:'button',
				text:'保存权限',
				iconCls:'save',
				handler:function(){
					var selectedRole= roleTree.getSelectionModel().getSelection();
					if(!selectedRole[0]){
						Ext.Msg.alert('系统提示', '请选择角色！');
						return false;
					}
					
                    var records = menuTree.getView().getChecked(),
                    ids = [];
	                Ext.Array.each(records, function(record){
	                    ids.push(record.get('id'));
	                });
        			Ext.Ajax.request({
        			    url:'/usercontrol/sysrole_authority_save/',
        			    method : 'post',
        			    params:{
        			    	role_id:selectedRole[0].data.id,
        			    	ids:ids.join(",").replace(/[^\d,]/g,'')
        			    },
        			    success:function(response){
        			        var results =  Ext.decode(response.responseText);
        			        if(results.success){
        			        	Ext.Msg.alert('系统提示', results.message);
        			        }else{
        			        	Ext.Msg.alert('系统提示', results.message);
        			        }
        			    }
        			});
				}
			}]			
		}],
		listeners : {
			'itemclick' : function(view, record, item, index, e) {
    			var params = { role_id:record.get('id') };
    		    Ext.apply(menuStore.proxy.extraParams, params);
    		    menuStore.load();
			},
			scope : this
		}
	});

	var roleForm = Ext.create('Ext.form.Panel', {
		width: 320,
		bodyPadding: 10,
	    fieldDefaults: {
	       labelAlign: 'right',
	       labelWidth: 60,
	       msgTarget: 'side'
	    },
	    layout: 'anchor',
		items : [{
			xtype:'hiddenfield',
			fieldLabel:'id',
			name:'id'
		},{
			xtype:'textfield',
			fieldLabel:'角色名称',
			name:'role_name',
			anchor:'100%',
			allowBlank: false
		}]
	});
		
	var roleWin = Ext.create('Ext.window.Window', {
		modal: true,
		closeAction: 'hide',
		items : [roleForm],
		plain : true,
		resizable : false,
		buttonAlign : 'center',
		buttons : [ {
			text : '保存',
			handler : function() {
				if (roleForm.getForm().isValid()) {
					roleForm.getForm().submit({
						waitTitle : '请稍候',
						waitMsg : '正在处理......',
						url : '/usercontrol/sysrole_save/',
						success : function(form, action) {
							roleWin.hide();
							roleStore.load();
						},
						failure : function(form, action) {
							if (action.result) {
								Ext.Msg.alert('系统提示', action.result.message);
							} 
						}
					});
				}
			}
		},{
			text : '取消',
			handler : function() {
				roleForm.getForm().reset();
				roleWin.hide();
			}
		}]
	}); 
	
	//menuTree
	var menuStore = Ext.create('Ext.data.TreeStore', {
		proxy: {
			type: 'ajax',
			url: '/usercontrol/sysrole_authority_list/',
	        actionMethods: {
	            read: 'POST'
	        }
		},
		autoLoad: true
	});
	
	var menuTree=Ext.create('Ext.tree.Panel',{
		title: '系统菜单',
		store: menuStore,
        rootVisible: false,
		split:true,
		region:'center',
		listeners : {  
	        'checkchange' : function(node,check){
			    node.cascadeBy(function(child){
			        child.set("checked",check);
			    });
	        }  
	    }
	});	
	

	var rolePanel=Ext.create('Ext.container.Viewport', {
		renderTo: 'role-panel',
		layout:'border',
		items:[roleTree,menuTree]
	});	
});